package com.canbot.u05.activity.attendance;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.button.MaterialButton;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.canbot.u05.R;
import com.canbot.u05.activity.BaseActivityNoTitle;
import com.canbot.u05.adapter.KqGroupAdapter;
import com.canbot.u05.bean.JsonResponse;
import com.canbot.u05.bean.KqGroup;
import com.canbot.u05.bean.PersonGroupData;
import com.canbot.u05.socket.IpMsgConst;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.OkHttpUtils;
import com.canbot.u05.utils.PathConst;
import com.canbot.u05.utils.SPUtils;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.widget.LoadingDialogUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 考勤信息显示界面  to_work_time   off_work_time  值 “09:00”
 */
public class AttendanceGroupActivity extends BaseActivityNoTitle implements View.OnClickListener, AdapterView.OnItemClickListener {

        private String[] fileName = new String[]{"u05_rlxx_001.wav", "u05_rlxx_002.wav", "u05_rlxx_003.wav"};

        private KqGroupAdapter adapter;

        private GridView gridView;

        private ImageView btnBack;

        private MaterialButton btnSure;

        private List<KqGroup> data;

        private KqGroup selectGroup;

        private int lastPosition = -1;

        private int groupId;

        private Dialog dialogLoading;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_kq_choose_group);
                setViews();
                int index = (int) (Math.random() * fileName.length);
                boolean isSuccess = MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.AUDIO_ATTENDANCE_DIR + fileName[index]);
                if (!isSuccess) {
                        ToastTools.newInstance(this).showText(getString(R.string.network_error));
                }

                groupId = SPUtils.getInt(this, "kq_groupId", -1);
                if (groupId != -1){
                        selectGroup = new KqGroup();
                        selectGroup.setGroupId(groupId);
                }
                getGroup();
        }

        private void getGroup() {
                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.data_upload));

                HashMap<String, Object> hasMap = new HashMap<>();
                hasMap.put("", "ed");

                OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST2 + ":7755/" + "faceGroupList", hasMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(final String result) {

                                Logger.e("save", "onSuccess:" + result);
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                ToastTools.newInstance(AttendanceGroupActivity.this).showText(getString(R.string.load_success));
                                                LoadingDialogUtils.closeDialog(dialogLoading);
                                                JsonResponse strMsgBean = JSON.parseObject(result, JsonResponse.class);
                                                if (strMsgBean.getRc() == 0) {
                                                        List<PersonGroupData> personGroup = JSON.parseArray(strMsgBean.getResult().toString(), PersonGroupData.class);
                                                        if (personGroup != null && personGroup.size() != 0) {
                                                                data = new ArrayList<KqGroup>();
                                                                KqGroup kqGroup;
                                                                for (int i = 0; i < personGroup.size(); i++) {
                                                                        kqGroup = new KqGroup();
                                                                        kqGroup.setGroupId(personGroup.get(i).getId());
                                                                        kqGroup.setGroupName(personGroup.get(i).getName());
                                                                        if (groupId == personGroup.get(i).getId()) {
                                                                                kqGroup.setChecked(true);
                                                                                lastPosition = i;
                                                                        }
                                                                        data.add(kqGroup);
                                                                }
                                                                setAdapter(data);
                                                        }
                                                }
                                        }
                                });
                        }

                        @Override
                        public void onFailure() {
                                Logger.e("save", "onFailure");
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                LoadingDialogUtils.closeDialog(dialogLoading);
                                                ToastTools.newInstance(AttendanceGroupActivity.this).showText(AttendanceGroupActivity.this.getString(R.string.network_error));
                                        }
                                });
                        }
                });
        }

        private void setAdapter(List<KqGroup> data) {
                adapter = new KqGroupAdapter(AttendanceGroupActivity.this, data);
                gridView.setAdapter(adapter);
        }

        /**
         * 用户考勤信息显示界面
         */
        private void setViews() {

                btnBack = (ImageView) findViewById(R.id.imageView_back);
                btnSure = (MaterialButton) findViewById(R.id.btn_sure);
                gridView = (GridView) findViewById(R.id.kq_group_gridView);

                btnBack.setOnClickListener(this);
                btnSure.setOnClickListener(this);
                gridView.setOnItemClickListener(this);
        }


        /**
         * 加载显示考勤信息
         */
        private void commitData() {
                dialogLoading = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.data_upload));
                Logger.e("save", "commitData:");
                HashMap<String, Object> hasMap = new HashMap<>();
                hasMap.put("groupId", selectGroup.getGroupId());


                OkHttpUtils.upLoadFile("http://" + IpMsgConst.SOCKET_HOST2 + ":7755/" + "settingKqGroup", hasMap, new OkHttpUtils.OkHttpCallBack() {
                        @Override
                        public void onSuccess(String result) {

                                Logger.e("save", "onSuccess:" + result);
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                ToastTools.newInstance(AttendanceGroupActivity.this).showText(AttendanceGroupActivity.this.getString(R.string.upload_complete));
                                                MsgSendUtils.sendStringMsg(MsgType.PLAY_SOUND, PathConst.AUDIO_ATTENDANCE_DIR + "u05_szcg_002.wav");
                                                LoadingDialogUtils.closeDialog(dialogLoading);
                                                if (selectGroup != null) {
                                                        SPUtils.saveInt(AttendanceGroupActivity.this, "kq_groupId", selectGroup.getGroupId());
                                                }

                                                finish();
                                                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
                                        }
                                });
                        }

                        @Override
                        public void onFailure() {
                                Logger.e("save", "onFailure");
                                runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                                ToastTools.newInstance(AttendanceGroupActivity.this).showText(AttendanceGroupActivity.this.getString(R.string.network_error));
                                                LoadingDialogUtils.closeDialog(dialogLoading);
                                        }
                                });
                        }
                });
        }

        @Override
        public void onClick(View view) {

                switch (view.getId()) {
                        case R.id.imageView_back:
                                finish();
                                startActivity(new Intent(AttendanceGroupActivity.this, AttendanceSettingTimeActivity.class));
                                overridePendingTransition(R.anim.face_zoom_enter, R.anim.face_zoom_exit);
                                break;

                        case R.id.btn_sure:
                                if (selectGroup == null)  {
                                        ToastTools.newInstance(AttendanceGroupActivity.this).showText("人脸信息库选择不能为空哦！");
                                        return;
                                }
                                commitData();
                                break;
                }
        }

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                if (data.get(position).isChecked()) {
                        data.get(position).setChecked(false);
                        adapter.updataView(position, gridView);
                        selectGroup = null;
                }
                else {
                        data.get(position).setChecked(true);
                        adapter.updataView(position, gridView);
                        selectGroup = data.get(position);
                }
                if (lastPosition != -1 && data.get(lastPosition).isChecked() && lastPosition != position) {
                        data.get(lastPosition).setChecked(false);
                        adapter.updataView(lastPosition, gridView);
                }
                lastPosition = position;

                if (selectGroup != null) {
                        Logger.e("t", "grouid = " + selectGroup.getGroupId());
                }
        }

        @Override
        protected void onPause() {
                if (dialogLoading != null && dialogLoading.isShowing()){
                        LoadingDialogUtils.closeDialog(dialogLoading);
                }
                super.onPause();
        }
}
